package com.lucifer.treasure.util;

import com.lucifer.treasure.ModLuciferTreasure;
import com.lucifer.treasure.config.ConfigHandler;

import java.util.Map;
import java.util.Arrays;
import java.util.List;

/**
 * 附魔配置管理器
 * 提供便捷的方法来管理附魔的启用/禁用状态
 */
public class EnchantConfigManager {
    
    // 所有可用的附魔名称列表
    public static final List<String> ALL_ENCHANT_NAMES = Arrays.asList(
        "auto_smelt",      // 自动熔炼
        "step",            // 自动台阶
        "steady",          // 稳定
        "experience_boost", // 经验提升
        "traveller",       // 旅行者
        "magnet",          // 磁力
        "excavation",      // 连锁挖掘
        "reach",           // 延伸打击
        "venom",           // 毒液
        "beheading",       // 斩首
        "disarm",          // 缴械
        "growth",          // 成长
        "life_leech",      // 生命汲取
        "beekeeper",       // 养蜂人
        "block_reach",     // 方块范围扩展
        "multibow",        // 多重射击
        "quickdraw",       // 快速拉弦
        "multijump",       // 多重跳跃
        "laststand",       // 最后一击
        "gloom_curse",     // 阴暗诅咒
        "ender_pearl"      // 末影珍珠
    );
    
    /**
     * 打印当前所有附魔的配置状态
     */
    public static void printAllEnchantStatus() {
        ModLuciferTreasure.LOGGER.info("=== 路西法的宝藏 Configuration Status ===");
        
        Map<String, Boolean> statusMap = ConfigHandler.getAllEnchantEnabledStatus();
        
        for (String enchantName : ALL_ENCHANT_NAMES) {
            boolean enabled = statusMap.getOrDefault(enchantName, true);
            String status = enabled ? "ENABLED" : "DISABLED";
            String displayName = getDisplayName(enchantName);
            
            ModLuciferTreasure.LOGGER.info("[{}] {} - {}", status, enchantName, displayName);
        }
        
        ModLuciferTreasure.LOGGER.info("============================================");
    }
    
    /**
     * 启用指定附魔
     */
    public static void enableEnchant(String enchantName) {
        if (ALL_ENCHANT_NAMES.contains(enchantName)) {
            ConfigHandler.setEnchantEnabled(enchantName, true);
            ModLuciferTreasure.LOGGER.info("Enabled enchantment: {}", enchantName);
        } else {
            ModLuciferTreasure.LOGGER.warn("Unknown enchantment: {}", enchantName);
        }
    }
    
    /**
     * 禁用指定附魔
     */
    public static void disableEnchant(String enchantName) {
        if (ALL_ENCHANT_NAMES.contains(enchantName)) {
            ConfigHandler.setEnchantEnabled(enchantName, false);
            ModLuciferTreasure.LOGGER.info("Disabled enchantment: {}", enchantName);
        } else {
            ModLuciferTreasure.LOGGER.warn("Unknown enchantment: {}", enchantName);
        }
    }
    
    /**
     * 启用所有附魔
     */
    public static void enableAllEnchants() {
        for (String enchantName : ALL_ENCHANT_NAMES) {
            ConfigHandler.setEnchantEnabled(enchantName, true);
        }
        ModLuciferTreasure.LOGGER.info("Enabled all enchantments");
    }
    
    /**
     * 禁用所有附魔
     */
    public static void disableAllEnchants() {
        for (String enchantName : ALL_ENCHANT_NAMES) {
            ConfigHandler.setEnchantEnabled(enchantName, false);
        }
        ModLuciferTreasure.LOGGER.info("Disabled all enchantments");
    }
    
    /**
     * 重置所有附魔配置为默认值（全部启用）
     */
    public static void resetToDefaults() {
        enableAllEnchants();
        ModLuciferTreasure.LOGGER.info("Reset all enchantment configurations to defaults");
    }
    
    /**
     * 获取附魔的显示名称（中文）
     */
    private static String getDisplayName(String enchantName) {
        return switch (enchantName) {
            case "auto_smelt" -> "自动熔炼";
            case "step" -> "自动台阶";
            case "steady" -> "稳定";
            case "experience_boost" -> "经验提升";
            case "traveller" -> "旅行者";
            case "magnet" -> "磁力";
            case "excavation" -> "连锁挖掘";
            case "reach" -> "延伸打击";
            case "venom" -> "毒液";
            case "beheading" -> "斩首";
            case "disarm" -> "缴械";
            case "growth" -> "成长";
            case "life_leech" -> "生命汲取";
            case "beekeeper" -> "养蜂人";
            case "block_reach" -> "方块范围扩展";
            case "multibow" -> "多重射击";
            case "quickdraw" -> "快速拉弦";
            case "multijump" -> "多重跳跃";
            case "laststand" -> "最后一击";
            case "gloom_curse" -> "阴暗诅咒";
            case "ender_pearl" -> "末影珍珠";
            default -> enchantName;
        };
    }
    
    /**
     * 获取所有启用的附魔数量
     */
    public static int getEnabledEnchantCount() {
        Map<String, Boolean> statusMap = ConfigHandler.getAllEnchantEnabledStatus();
        return (int) ALL_ENCHANT_NAMES.stream()
                .mapToLong(name -> statusMap.getOrDefault(name, true) ? 1 : 0)
                .sum();
    }
    
    /**
     * 获取所有禁用的附魔数量
     */
    public static int getDisabledEnchantCount() {
        return ALL_ENCHANT_NAMES.size() - getEnabledEnchantCount();
    }
    
    /**
     * 检查是否有任何附魔被禁用
     */
    public static boolean hasDisabledEnchants() {
        return getDisabledEnchantCount() > 0;
    }
} 